package com.meiyou.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.meiyou.mapper.ChongzhiMapper;
import com.meiyou.mapper.ChongzhiMapperCustom;
import com.meiyou.mapper.MeiyouChongzhiMapper;
import com.meiyou.mapper.MeiyouUserMapper;
import com.meiyou.mapper.UserMapper;
import com.meiyou.mapper.UserMapperCustom;
import com.meiyou.pojo.Chongzhi;
import com.meiyou.pojo.MeiyouChongzhi;
import com.meiyou.pojo.User;
import com.meiyou.pojo.UserExample;
import com.meiyou.pojo.UserExample.Criteria;
import com.meiyou.service.ChongzhiService;
@Service("ChongzhiService")
public class ChongzhiServiceImpl implements ChongzhiService {

	@Autowired
	UserMapper userMapper;
	@Autowired
	ChongzhiMapper chongzhiMapper;
	@Autowired
	UserMapperCustom userMapperCustom;
    @Autowired
    ChongzhiMapperCustom chongzhiMapperCustom;
    @Autowired
    MeiyouUserMapper meiyouUserMapper;
    
	
	@Autowired
	MeiyouChongzhiMapper meiyouchongzhiMapper;
	public int addChongzhijilu(Chongzhi chongzhi) {
		// TODO Auto-generated method stub
		
		return chongzhiMapper.insert(chongzhi); 
	}

	
	public List<Chongzhi> selectChongzhijilu(Chongzhi chongzhi) {
		// TODO Auto-generated method stub
		
		return chongzhiMapperCustom.selectAll(chongzhi);  
	}

	
	public double selectMeijin(String username) {
		// TODO Auto-generated method stub
		UserExample example = new UserExample();
		Criteria criteria =  example.createCriteria();
		criteria.andUCountEqualTo(username);
		List<User> list = userMapper.selectByExample(example); 
		return list.get(0).getuMeijin(); 
	}

	
	public int updateMeijin(User user) {
		// TODO Auto-generated method stub
		
		return userMapperCustom.updateMeijin(user);
	}

	
	public int delete(int id) {
		// TODO Auto-generated method stub
		return chongzhiMapper.deleteByPrimaryKey(id+"");
	}


	public int updateState(MeiyouChongzhi chongzhi) {
		
		// TODO Auto-generated method stub
		if(meiyouchongzhiMapper.updateByPrimaryKey(chongzhi)>=1) {
			
			
			if(meiyouUserMapper.updAddMeijin(chongzhi.getcMeijin(), chongzhi.getcUCount())>=1) {
				return 1;
			}
			
		}
		return 0;
		
		

	}
}
